package com.tsy.bluetooth.utils;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tsy.bluetooth.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class AdapterDemo extends RecyclerView.Adapter {
    private List<BluetoothDevice> devices;

    private Context mContext;
    public AdapterDemo(Context context, Set<BluetoothDevice> devices){
        mContext = context;
//        this.devices = devices;
        Iterator<BluetoothDevice> deviceIterator = devices.iterator();
        this.devices = new ArrayList<>();
        while (deviceIterator.hasNext()) {
            BluetoothDevice device = deviceIterator.next();
            this.devices.add(device);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_device,parent,false);
        return new RecyclerView.ViewHolder(view) {
            @Override
            public String toString() {
                return super.toString();
            }
        };
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TextView tvDevice = holder.itemView.findViewById(R.id.tv_device);
        BluetoothDevice device = devices.get(position);
        tvDevice.setText(device.getName());
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }
}
